home *** CD-ROM | disk | FTP | other *** search
/ PC World Komputer 2010 April / PCWorld0410.iso / hity wydania / Ubuntu 9.10 PL / karmelkowy-koliberek-9.10-netbook-remix-PL.iso / casper / filesystem.squashfs / usr / lib / python2.6 / asynchat.pyc (.txt) < prev    next >
Encoding:
Python Compiled Bytecode  |  2009-11-11  |  8.5 KB  |  267 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyc (Python 2.6)
  3.  
  4. """A class supporting chat-style (command/response) protocols.
  5.  
  6. This class adds support for 'chat' style protocols - where one side
  7. sends a 'command', and the other sends a response (examples would be
  8. the common internet protocols - smtp, nntp, ftp, etc..).
  9.  
  10. The handle_read() method looks at the input stream for the current
  11. 'terminator' (usually '\\r\\n' for single-line responses, '\\r\\n.\\r\\n'
  12. for multi-line output), calling self.found_terminator() on its
  13. receipt.
  14.  
  15. for example:
  16. Say you build an async nntp client using this class.  At the start
  17. of the connection, you'll have self.terminator set to '\\r\\n', in
  18. order to process the single-line greeting.  Just before issuing a
  19. 'LIST' command you'll set it to '\\r\\n.\\r\\n'.  The output of the LIST
  20. command will be accumulated (using your own 'collect_incoming_data'
  21. method) up to the terminator, and then control will be returned to
  22. you - by calling your self.found_terminator() method.
  23. """
  24. import socket
  25. import asyncore
  26. from collections import deque
  27. from sys import py3kwarning
  28. from warnings import filterwarnings, catch_warnings
  29.  
  30. class async_chat(asyncore.dispatcher):
  31.     '''This is an abstract class.  You must derive from this class, and add
  32.     the two methods collect_incoming_data() and found_terminator()'''
  33.     ac_in_buffer_size = 4096
  34.     ac_out_buffer_size = 4096
  35.     
  36.     def __init__(self, sock = None, map = None):
  37.         self.ac_in_buffer = ''
  38.         self.incoming = []
  39.         self.producer_fifo = deque()
  40.         asyncore.dispatcher.__init__(self, sock, map)
  41.  
  42.     
  43.     def collect_incoming_data(self, data):
  44.         raise NotImplementedError('must be implemented in subclass')
  45.  
  46.     
  47.     def _collect_incoming_data(self, data):
  48.         self.incoming.append(data)
  49.  
  50.     
  51.     def _get_data(self):
  52.         d = ''.join(self.incoming)
  53.         del self.incoming[:]
  54.         return d
  55.  
  56.     
  57.     def found_terminator(self):
  58.         raise NotImplementedError('must be implemented in subclass')
  59.  
  60.     
  61.     def set_terminator(self, term):
  62.         '''Set the input delimiter.  Can be a fixed string of any length, an integer, or None'''
  63.         self.terminator = term
  64.  
  65.     
  66.     def get_terminator(self):
  67.         return self.terminator
  68.  
  69.     
  70.     def handle_read(self):
  71.         
  72.         try:
  73.             data = self.recv(self.ac_in_buffer_size)
  74.         except socket.error:
  75.             why = None
  76.             self.handle_error()
  77.             return None
  78.  
  79.         self.ac_in_buffer = self.ac_in_buffer + data
  80.         while self.ac_in_buffer:
  81.             lb = len(self.ac_in_buffer)
  82.             terminator = self.get_terminator()
  83.             if not terminator:
  84.                 self.collect_incoming_data(self.ac_in_buffer)
  85.                 self.ac_in_buffer = ''
  86.                 continue
  87.             if isinstance(terminator, int) or isinstance(terminator, long):
  88.                 n = terminator
  89.                 if lb < n:
  90.                     self.collect_incoming_data(self.ac_in_buffer)
  91.                     self.ac_in_buffer = ''
  92.                     self.terminator = self.terminator - lb
  93.                 else:
  94.                     self.collect_incoming_data(self.ac_in_buffer[:n])
  95.                     self.ac_in_buffer = self.ac_in_buffer[n:]
  96.                     self.terminator = 0
  97.                     self.found_terminator()
  98.             lb < n
  99.             terminator_len = len(terminator)
  100.             index = self.ac_in_buffer.find(terminator)
  101.             if index != -1:
  102.                 if index > 0:
  103.                     self.collect_incoming_data(self.ac_in_buffer[:index])
  104.                 
  105.                 self.ac_in_buffer = self.ac_in_buffer[index + terminator_len:]
  106.                 self.found_terminator()
  107.                 continue
  108.             index = find_prefix_at_end(self.ac_in_buffer, terminator)
  109.             if index:
  110.                 if index != lb:
  111.                     self.collect_incoming_data(self.ac_in_buffer[:-index])
  112.                     self.ac_in_buffer = self.ac_in_buffer[-index:]
  113.                 
  114.                 break
  115.                 continue
  116.             self.collect_incoming_data(self.ac_in_buffer)
  117.             self.ac_in_buffer = ''
  118.  
  119.     
  120.     def handle_write(self):
  121.         self.initiate_send()
  122.  
  123.     
  124.     def handle_close(self):
  125.         self.close()
  126.  
  127.     
  128.     def push(self, data):
  129.         sabs = self.ac_out_buffer_size
  130.         if len(data) > sabs:
  131.             for i in xrange(0, len(data), sabs):
  132.                 self.producer_fifo.append(data[i:i + sabs])
  133.             
  134.         else:
  135.             self.producer_fifo.append(data)
  136.         self.initiate_send()
  137.  
  138.     
  139.     def push_with_producer(self, producer):
  140.         self.producer_fifo.append(producer)
  141.         self.initiate_send()
  142.  
  143.     
  144.     def readable(self):
  145.         '''predicate for inclusion in the readable for select()'''
  146.         return 1
  147.  
  148.     
  149.     def writable(self):
  150.         '''predicate for inclusion in the writable for select()'''
  151.         if not self.producer_fifo:
  152.             pass
  153.         return not (self.connected)
  154.  
  155.     
  156.     def close_when_done(self):
  157.         '''automatically close this channel once the outgoing queue is empty'''
  158.         self.producer_fifo.append(None)
  159.  
  160.     
  161.     def initiate_send(self):
  162.         while self.producer_fifo and self.connected:
  163.             first = self.producer_fifo[0]
  164.             if not first:
  165.                 del self.producer_fifo[0]
  166.                 if first is None:
  167.                     self.handle_close()
  168.                     return None
  169.             
  170.             obs = self.ac_out_buffer_size
  171.             
  172.             try:
  173.                 catch_warnings().__enter__()
  174.                 
  175.                 try:
  176.                     data = buffer(first, 0, obs)
  177.                 finally:
  178.                     pass
  179.  
  180.             except TypeError:
  181.                 data = first.more()
  182.                 if data:
  183.                     self.producer_fifo.appendleft(data)
  184.                     continue
  185.                 del self.producer_fifo[0]
  186.                 continue
  187.  
  188.             
  189.             try:
  190.                 num_sent = self.send(data)
  191.             except socket.error:
  192.                 self.handle_error()
  193.                 return None
  194.  
  195.             if num_sent:
  196.                 if num_sent < len(data) or obs < len(first):
  197.                     self.producer_fifo[0] = first[num_sent:]
  198.                 else:
  199.                     del self.producer_fifo[0]
  200.             
  201.             return None
  202.  
  203.     
  204.     def discard_buffers(self):
  205.         self.ac_in_buffer = ''
  206.         del self.incoming[:]
  207.         self.producer_fifo.clear()
  208.  
  209.  
  210.  
  211. class simple_producer:
  212.     
  213.     def __init__(self, data, buffer_size = 512):
  214.         self.data = data
  215.         self.buffer_size = buffer_size
  216.  
  217.     
  218.     def more(self):
  219.         if len(self.data) > self.buffer_size:
  220.             result = self.data[:self.buffer_size]
  221.             self.data = self.data[self.buffer_size:]
  222.             return result
  223.         result = self.data
  224.         self.data = ''
  225.         return result
  226.  
  227.  
  228.  
  229. class fifo:
  230.     
  231.     def __init__(self, list = None):
  232.         if not list:
  233.             self.list = deque()
  234.         else:
  235.             self.list = deque(list)
  236.  
  237.     
  238.     def __len__(self):
  239.         return len(self.list)
  240.  
  241.     
  242.     def is_empty(self):
  243.         return not (self.list)
  244.  
  245.     
  246.     def first(self):
  247.         return self.list[0]
  248.  
  249.     
  250.     def push(self, data):
  251.         self.list.append(data)
  252.  
  253.     
  254.     def pop(self):
  255.         if self.list:
  256.             return (1, self.list.popleft())
  257.         return (0, None)
  258.  
  259.  
  260.  
  261. def find_prefix_at_end(haystack, needle):
  262.     l = len(needle) - 1
  263.     while l and not haystack.endswith(needle[:l]):
  264.         l -= 1
  265.     return l
  266.  
  267.